<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/includes.jsp"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>退货编辑</title>
<!-- import css and js -->
<%@ include file="/common/metaInput.jsp"%>
<script src="${ctx}/js/jsp/comm.js" type="text/javascript"></script>
<script type="text/javascript" src="${ctx}/js/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/js/jsp/yd/logic/ItemStateData.js"></script>
<script src="${ctx}/js/jsp/date.js" type="text/javascript"></script>
<script src="${ctx}/js/print/LodopFuncs.js" type="text/javascript"></script>
<script type="text/javascript">
	var basic_Url = "${ctx}/logic/materialback";
	var addressGrid;
	var print=false;
	$(function() {
		var column1 = [ { display: 'id', name: 'id',type: 'string',hide:true},
					    { display: '物料id', name: 'zd002MaterId',width:1,hide:true},
					    { display: '<font color="gray">物料编码</font>', name: 'zd002MaterCode', width: 120 },
					    { display: '<font color="gray">名称</font>', name: 'zd002MaterName',width: 120},
					    { display: '<font color="gray">型号</font>', name: 'zd002MaterModel' ,width: 120},
					    { display: '<font color="gray">尺寸</font>', name: 'zd002MaterSpec', width: 100},
					    { display: '<font color="gray">颜色</font>', name: 'zd002MaterColor', width: 100},
		                { display: '<font color="gray">材质</font>', name: 'zd002MaterQuanlity', width: 100}, 
		                { display: '<font color="gray">单位</font>', name: 'zd003MaterUnit' ,width: 100},
		                { display: '<font color="gray">单价</font>', name: 'zd004MaterPrice' ,width: 100},
		                //{ display: '<font color="gray">订单数量</font>', name: 'zd005OrderQuantity',width: 100 },
		                //{ display: '<font color="gray">已入库数量</font>', name: 'zd006HsSgQuantity',width: 100 },
		                { display: '<font color="gray">入库/出库数量</font>', name: 'zd007SgQuantity',width: 100 },
					    { display: '<font color="gray">已退货数量</font>', name: 'zd009HsBackQuantity',width: 100 },
					    { display: '退货数量<font color="red">*</font>', name: 'zd010TheBackQuantity',editor : {type :'float'},width: 150},
					    { display: '描述', name: 'zd011Descript',editor : {type :'text'},width: 150}
					  ];
		var column2 = [ { display: 'id', name: 'id',type: 'string',hide:true},
					    { display: '物料id', name: 'zd002MaterId',width:1,hide:true},
					    { display: '<font color="gray">物料编码</font>', name: 'zd002MaterCode', width: 120 },
					    { display: '<font color="gray">名称</font>', name: 'zd002MaterName',width: 120},
					    { display: '<font color="gray">型号</font>', name: 'zd002MaterModel' ,width: 120},
					    { display: '<font color="gray">尺寸</font>', name: 'zd002MaterSpec', width: 100},
					    { display: '<font color="gray">颜色</font>', name: 'zd002MaterColor', width: 100},
		                { display: '<font color="gray">材质</font>', name: 'zd002MaterQuanlity', width: 100}, 
		                { display: '<font color="gray">单位</font>', name: 'zd003MaterUnit' ,width: 100},
		                { display: '<font color="gray">单价</font>', name: 'zd004MaterPrice' ,width: 100},
		                //{ display: '<font color="gray">订单数量</font>', name: 'zd005OrderQuantity',width: 100 },
		                //{ display: '<font color="gray">已入库数量</font>', name: 'zd006HsSgQuantity',width: 100 },
		                { display: '<font color="gray">入库/出库数量</font>', name: 'zd007SgQuantity',width: 100 },
					    { display: '<font color="gray">已退货数量</font>', name: 'zd009HsBackQuantity',width: 100 },
					    { display: '退货数量<font color="red">*</font>', name: 'zd010TheBackQuantity',width: 150},
					    { display: '描述', name: 'zd011Descript',width: 150}
					  ];
		var column3 = column1;   
	    //按钮可见
		var billState = '${materBack.zd003Statue}';//单据的状态
		if(''==billState && 'ADD'=='${flag}'){
			//新增
			column3 = column1;
		}else{
			if('MRCK_SAVE'==billState||'MRCK_BACK'==billState){
				column3 = column1;
		    }else{
		    	column3 = column2;
		    }
		}
		addressGrid = $("#dataGrid1").ligerGrid({
			columns: column3,
            enabledEdit: true,
           	usePager: true,pageSize:1000,pageSizeOptions:[1000],
           	parms:[{ name : 'backId' , value : '${materBack.id}'}],
           	url : basic_Url+"/gridItems.do",
           	width: "100%", height:'90%', checkbox: true,
           	toolbar: { items: [{ text: '删除', click: deleteRow, img: '${ctx}/js/ligerUI/skins/icons/delete.gif' }]}
        });
		
		$("#pageloading").hide();
		//提交
		$("#inputForm").validate({
			onsubmit : true,
			/*执行ajaxsubmit  */
			submitHandler: function(editform) {
		        try{			    		
		    		var obj={'zd001Code':$("#zd001Code").val(),'zd005OriCode':$("#zd005OriCode").val(),'zd004Date':$("#zd004Date").val(),'zd006WhName':$("#zd006WhName").val(),'zd004EntryDate':$("#zd004EntryDate").val(),'zd007PiCode':$("#zd007PiCode").val(),'zd008CompName':$("#zd008CompName").val(),'userZHName':$("#userZHName").val()};
		            url =  basic_Url+"/save.do";
		            var api = frameElement.api;
		        	var W = api.opener;
		        	getReturnMessage(basic_Url+"/save.do","inputForm",W.searchAP);
		        	//getReturnMessageThree(url, "inputForm", W.searchAP,'',obj,addressGrid.getData(),print,"checkBack");
		        	print=false;
		        }catch(e){alert(e);}
		    }
		});
		if(billState=='' || '${flag}'=='ADD'){
			//新增
			$("#forSubmit").hide();
			$("#forBack").hide();
			$("#forAudit").hide();
			$("#print1").hide();
			$("#print2").hide();
		}else if(billState=='MRCK_SAVE'){
			//暂存
			$("#forBack").hide();
			$("#forAudit").hide();
		}else if(billState=='MRCK_COMP'){
			$("#forSave").hide();
			$("#forSubmit").hide();
		}else if(billState=='MRCK_AUDIT'){
			//已提交待二审
			$("#forSave").hide();
			$("#forSubmit").hide();
			$("#forAudit").hide();
			$("#forBack").hide();
		}else if(billState=='MRCK_BACK'){
			$("#forBack").hide();
			$("#forAudit").hide();
		}
	});
	//删除
	function deleteRow(){ 
		addressGrid.deleteSelectedRow();
	}
	//表单提交
	function submitForm(param) {
		var data = $("#dataGrid1").ligerGetGridManager().getData();
		var dele = $("#dataGrid1").ligerGetGridManager().getDeleted();
		if(data.length==0){
			$.dialog.alert("请增加物料信息");
	   	 	return false;
		}
		var ba = true;
		if(data!=null){ 
			for(var i=0; i<data.length; i++){
				var row = data[i]; 
				i++;
				if(isNaN(row.zd010TheBackQuantity)||row.zd010TheBackQuantity==undefined||row.zd010TheBackQuantity==null||row.zd010TheBackQuantity<=0){
					ba=false;
					$.dialog.alert("您第" + i + "行的退货数量没有填写,或者填写错误(数量不能为0)");
					return false;
				}
				if(parseFloat(parseFloat(row.zd007SgQuantity - row.zd009HsBackQuantity)).toFixed(4)< parseFloat(parseFloat(row.zd010TheBackQuantity).toFixed(4))){
					ba = false;
					$.dialog.alert("您第" + i + "行的退货数量大于订单未退货数量");
					return false;
				}
			}
		}
		//设置ajax全局同步
		jQuery.ajaxSettings.async = false;
		if("${materBack.zd003Statue}"=='MRCK_COMP'){
			print=true;
		}
		if(ba){
			$("#flagMark").val(param);
			$("#itemdata").val(JSON.stringify(data));
			$("#itemdete").val(JSON.stringify(dele));
			$("#inputForm").submit();
		}
	}
	//关闭窗口
	function close(){
		var api = frameElement.api;
		var W = api.opener;
		CloseDialog();
		W.searchAP();
	}
	//入库单据选择
	function zdOriCodeWindow(){
		getDG().dialog({
			title : '选择单据',
			width : 750,
			height : 350,
			content : 'url:' + sys_context + '/public/windowalert/billTypeSel.do?zdType='+'',    
			lock : true,
			max : true,
			min : true,
			ok : function() {
				var rows = this.content.getSelected();
				if(rows==null){
					$.dialog.alert("请选择数据");
					return;
				}
				else{
					if(rows.length>1){
						$.dialog.alert('只能选择一行数据!');
						return;
					}else{
						$(rows).each(function() {
							$("#zd005OriId").val(this.zd001Id);
							$("#zd005OriCode").val(this.zd001Code);
							$("#zd006WhId").val(this.zd002WhId);
							$("#zd006WhCode").val(this.zd002WhCode);
							$("#zd006WhName").val(this.zd002WhName);
							$("#zd008CompId").val(this.zd004CompId);
							$("#zd008CompCode").val(this.zd004CompCode);
							$("#zd008CompName").val(this.zd004CompName);
							$("#zd002Type").val(this.zd006BillType);
							var entryDate=new Date(this.zd003Date);
							$("#zd004EntryDate").val(formatDate(entryDate,"yyyy-MM-dd"));
							$("#zd007PiCode").val(this.zd005PiCode);
							
							multipleSearch("#dataGrid1",true,"",basic_Url+"/materialItems.do?piCode="+$("#zd007PiCode").val()+"&billId="+$("#zd005OriId").val()+"&billType="+$("#zd002Type").val());	
					  });
					}
				}
				this.content.Close();
				return false;
			},
			cancelVal : '关闭',
			cancel : true
		}).zindex();
	}
/* 	function PrintView(type){
		var data = addressGrid.getData();//获取单据要打印物料
		var obj={'zd001Code':$("#zd001Code").val(),'zd005OriCode':$("#zd005OriCode").val(),'zd004Date':$("#zd004Date").val(),'zd006WhName':$("#zd006WhName").val(),'zd004EntryDate':$("#zd004EntryDate").val(),'zd007PiCode':$("#zd007PiCode").val(),'zd008CompName':$("#zd008CompName").val(),'userZHName':$("#userZHName").val()};
		var isprint=false;
		if(type==2){//打印
			isprint=true;	
		}
		window.parent.parent.loadPrint(obj,data,"checkBack",isprint);
	}
	 */
</script>
</head>
<body>
	<input id="userZHName" name="userZHName" type="hidden" value="${userZHName }" />
	<form id="inputForm" action="" method="post">
		<input type="hidden" id="id" name="id" value="${materBack.id}" />
		<input type="hidden" id="itemdata" name="itemdata"/>
		<input type="hidden" id="itemdete" name="itemdete"/>
		<input type="hidden" id="flagMark" name="flagMark"/>
		<input type="hidden" id="zd002Type" name="zd002Type" value="${materBack.zd002Type }"/>
		<input type="hidden" id="bizVersion" name="bizVersion" value="${materBack.bizVersion}"/>
		<!-- 用于标识提交按钮 -->
		<input type="hidden" id="submitFlag" value="" />
		<div class="pageContent">
			<div class="panel">
				<table class="TableBlock" align="center">
					<tr>
						<c:choose>
		                	<c:when test="${not empty materBack.zd001Code}">
		                    	<td nowrap>退货单号:</td>
			                    <td>
			   		             	<input type="text" id="zd001Code" name="zd001Code" class="inp_width_98p" readonly="readonly" value="${materBack.zd001Code}" />
			   		           	</td>
			   		            <td colspan="2"></td>
		                    </c:when>
		                    <c:otherwise>
		                   	</c:otherwise>
	              	</c:choose>
	              	</tr>
					<tr>
						<td>入库/出库单号</td>
						<td>
							<input type="hidden" id="zd005OriId" name="zd005OriId" value="${materBack.zd005OriId }" />
							<input type="text" id="zd005OriCode" name="zd005OriCode" class="required" style="width:200px;" value="${materBack.zd005OriCode }" readonly="readonly"/>
							<input type="button" value="..." onclick="zdOriCodeWindow();" />
						</td>
						<td>退货日期</td>
						<td>
							<input id="zd004Date" name="zd004Date" class="inp_width_98p required" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" value="<fmt:formatDate value="${materBack.zd004Date}" pattern="yyyy-MM-dd" type="both" />" />
						</td>
					</tr>
					<tr>
						<td>出库仓库:</td>
						<td>
							<input type="hidden" style="width:200px;" id="zd006WhId" name="zd006WhId" value="${materBack.zd006WhId }" />
                   		 	<input type="hidden" id="zd006WhCode" name="zd006WhCode" value="${materBack.zd006WhCode }"/>
  		              		<input type="text" id="zd006WhName" name="zd006WhName" value="${materBack.zd006WhName }" class="inp_width_98p required" readonly="readonly"/>
						</td>
						<td>出库/入库时间:</td>
						<td>
							<input id="zd004EntryDate" name="zd004EntryDate" class="inp_width_98p required" onclick="WdatePicker({dateFmt:'yyyy-MM-dd'})" value="<fmt:formatDate value="${materBack.zd004EntryDate}" pattern="yyyy-MM-dd" type="both" />" />
						</td>
					</tr>
					<tr>
					 	<td>PI单号:</td>
						<td>
   		                     <input type="text" id="zd007PiCode" name="zd007PiCode" class="inp_width_98p required" value="${materBack.zd007PiCode}" readonly="readonly"/>
						</td>
						<td>客户/供应商:</td>
						<td>
   		                    <input type="hidden" style="width:200px;" id="zd008CompId" name="zd008CompId" value="${materBack.zd008CompId }" />
                   		 	<input type="hidden" id="zd008CompCode" name="zd008CompCode" value="${materBack.zd008CompCode }"/>
  		              		<input type="text" id="zd008CompName" name="zd008CompName" value="${materBack.zd008CompName }" class="inp_width_98p" readonly="readonly"/>
						</td>
					</tr>
					<tr>
						<td>制单人:</td>
						<td >
						    <input type="text" id="zd009Aegnt" name="zd009Aegnt" class="inp_width_98p" value="${user.name}" readonly="readonly"/>
							<c:if test="${materBack.zd009Aegnt != null}">
								<script>
									$("#zd009Aegnt").val("${materBack.zd009Aegnt}");
								</script>
							</c:if>
					  </td>
						<td>备注:</td>
						<td>
							<input id="zd011ReMark" name="zd011ReMark" class="inp_width_98p" value="${materBack.zd011ReMark}" />
						</td>
					</tr>
					<tr>
						<td colspan="4" style="margin-bottom: 10px;">
							<div id="dataGrid1"></div>
						</td>
					</tr>
				</table>
			</div>
			<div class="formBar">
				<ul>
					<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forSave" onclick="submitForm('MRCK_SAVE')">保存</button></div></div></li>
					<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forSubmit" onclick="submitForm('MRCK_COMP')">提交</button></div></div></li>
		        	<webres:displaycontroller elementId="LOGIC_MATERIALBACK_CK_SP" description="退货单审批">
			    		<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forAudit" onclick="submitForm('MRCK_AUDIT')">审核通过</button></div></div></li>
			    	</webres:displaycontroller>
			    	<webres:displaycontroller elementId="LOGIC_MATERIALBACK_CK_PH" description="退货单驳回">   
			       		<li><div class="buttonActive"><div class="buttonContent"><button type="button" id="forBack" onclick="submitForm('MRCK_BACK')">驳回</button></div></div></li>
		       		</webres:displaycontroller>
		       		<li><div class="button"><div class="buttonContent"><button type="button" class="print" onclick="CloseDialog()">关闭</button></div></div></li>
				</ul>
			</div>
		</div>
	</form>
</body>
</html>
